package cn.edu.fzu.homemaking.company.controller;

import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.company.domain.entity.CompanyUser;
import cn.edu.fzu.homemaking.company.domain.entity.RobotMsg;
import cn.edu.fzu.homemaking.company.domain.enums.RobotMsgStatusEnum;
import cn.edu.fzu.homemaking.company.service.RobotMsgService;
import cn.edu.fzu.homemaking.company.util.UserInfoUtil;
import cn.edu.fzu.homemaking.web.Result;
import cn.edu.fzu.homemaking.web.RetCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wanjie
 * @version 1.0
 */
@RestController
@RequestMapping("/api/company/auth/robotMsg")
@Api(tags = "公司端定制消息需求相关接口")
public class RobotMsgController {
    @Resource
    private RobotMsgService robotMsgService;

    @PostMapping("/insert")
    @ApiOperation(value = "新增回复模板", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "key", value = "定制消息关键字", required = true),
            @ApiImplicitParam(name = "message", value = "定制消息具体内容", required = true),
            @ApiImplicitParam(name = "status", value = "定制消息具体状态", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result insert(String key, String message, RobotMsgStatusEnum status, HttpServletRequest request) {
        CompanyUser companyUser = UserInfoUtil.getUserInfo(request, false);
        //取出companyId
        Long companyId = companyUser.getCompany().getId();
        robotMsgService.insertRobotMsg(key, message, status, companyId);
        return new Result(RetCode.SUCCESS, "新增回复模板成功");
    }


    @GetMapping("/list")
    @ApiOperation(value = "定制信息展示列表", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "key", value = "定制消息关键字"),
            @ApiImplicitParam(name = "message", value = "定制消息具体内容"),
            @ApiImplicitParam(name = "status", value = "定制消息具体状态"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result list(String key, String message, RobotMsgStatusEnum status, Integer pageNum, Integer pageSize,
                       HttpServletRequest request) {
        CompanyUser companyUser = UserInfoUtil.getUserInfo(request, false);
        //取出companyId
        Long companyId = companyUser.getCompany().getId();
        try (Page<List<RobotMsg>> page = PageHelper.startPage(pageNum, pageSize)) {
            robotMsgService.queryAllRobotMsgs(companyId, key, message, status);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }


    @GetMapping("listByCompany")
    @ApiOperation(value = "根据公司id查询定制信息展示列表--前端请勿调用", httpMethod = "GET")
    public Result listByCompany(Long companyId) {
        return new Result(robotMsgService.queryAllRobotMsgs(companyId, null, null, RobotMsgStatusEnum.NORMAL));
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改定制信息", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", required = true),
            @ApiImplicitParam(name = "key", value = "定制消息关键字"), @ApiImplicitParam(name = "message", value = "定制消息具体内容"),
            @ApiImplicitParam(name = "status", value = "定制消息具体状态", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result updateDemandStatus(Long id, String key, String message, RobotMsgStatusEnum status,
                                     HttpServletRequest request) {
        CompanyUser companyUser = UserInfoUtil.getUserInfo(request, false);
        //取出companyId
        Long companyId = companyUser.getCompany().getId();
        robotMsgService.updateRobotMsg(id, key, message, status, companyId);
        return new Result(RetCode.SUCCESS, "更新回复模板成功");
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除定制信息", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result delete(Long id, HttpServletRequest request) {
        CompanyUser companyUser = UserInfoUtil.getUserInfo(request, false);
        //取出companyId
        Long companyId = companyUser.getCompany().getId();
        robotMsgService.deleteRobotMsg(id, companyId);
        return new Result(RetCode.SUCCESS, "删除模板成功");
    }

}
